package com.my.screenadapter.mdir;

import java.io.*;

/**
 * @author Ajie
 * @date 2019-11-07
 * @function
 */
public class MFileUtils {

    public static void main(String args[]) {
//        FileLocation.
//        File file = new File();


        File abc = new File("abc");
//        ************ del file ***************
//        boolean mkdir = abc.mkdir();
//        File file = new File("abc/bcd");
//        AppendFile.method1(file.getAbsolutePath(),"bcd");
//        MFileUtils t = new MFileUtils();
//        delAll("D:/delTemp");
//        delAll("D:/readme.txt");
//        System.out.println("deleteted");
//        delAll(abc.getAbsolutePath());


//        ************* append File **************
//        try{
//            File file = new File("./text.txt");
//            if(file.createNewFile()){
//                System.out.println("Create file successed");
//            }
//            appendFileBufferWtiter("./text.txt", "111-");
//            appendFileBufferWtiter("./text.txt", "\r\n");
//            String name = file.getName();
//            System.out.println("append file path: "+file.getAbsolutePath()+"    filename:"+ name);
//            if (name.contains(".")){
//                name = name.substring(0,name.lastIndexOf("."));
//            }
//            System.out.println("name "+name);
//            appendFileBufferWtiter("./text.txt", "22222");
//            appendFileBufferWtiter("./text.txt", "\r\n");
//            appendFileWriter("./text.txt", "aaaaaa");
//            appendFileWriter("./text.txt", "\r\n");
//            appendFileWriter("./text.txt", "456");
//            appendFileRandomAccess("./text.txt", "789");
//            appendFileRandomAccess("./text.txt", "\r\n");
//        }catch(Exception e){
//            System.out.println(e);
//        }


    }

    // 删除文件夹
    // param folderPath 文件夹完整绝对路径
    public static void delAll(String folderPath) {
        if (folderPath == null || folderPath.length()==0){
            return;
        }
        try {
            boolean b = delFilesExcludePathFolder(folderPath);// 删除完里面所有内容
            System.out.println("del all res "+b);
            String filePath = folderPath;
            filePath = filePath.toString();
            java.io.File myFilePath = new java.io.File(filePath);
            System.out.println("del file null folder "+myFilePath.getAbsolutePath());
            boolean delete = myFilePath.delete();// 删除空文件夹
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 删除指定文件夹下所有文件
    // param path 文件夹完整绝对路径
    public static boolean delFilesExcludePathFolder(String path) {
        if (path == null || path.length()==0){
            return true;
        }
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return true;
        }
        if (file.isFile()){
            return file.delete();
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; tempList!=null && i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                System.out.println("del file =="+temp.getAbsolutePath());
                temp.delete();
            }
            if (temp.isDirectory()) {
                delFilesExcludePathFolder(path + File.separator + tempList[i]);// 先删除文件夹里面的文件
                delAll(path + File.separator + tempList[i]);// 再删除空文件夹
                flag = true;
            }
        }
        return flag;
    }

    public static void appendFileBufferWtiter(String file, String conent) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
            out.write(conent);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(out != null){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 追加文件：使用FileWriter
     *
     * @param fileName
     * @param content
     */
    public static void appendFileWriter(String fileName, String content) {
        FileWriter writer = null;
        try {
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            writer = new FileWriter(fileName, true);
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(writer != null){
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 追加文件：使用RandomAccessFile
     *
     * @param fileName 文件名
     * @param content 追加的内容
     */
    public static void appendFileRandomAccess(String fileName, String content) {
        RandomAccessFile randomFile = null;
        try {
            // 打开一个随机访问文件流，按读写方式
            randomFile = new RandomAccessFile(fileName, "rw");
            // 文件长度，字节数
            long fileLength = randomFile.length();
            // 将写文件指针移到文件尾。
            randomFile.seek(fileLength);
            randomFile.writeBytes(content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if(randomFile != null){
                try {
                    randomFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
